package org.duang.kit;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class ConvertToPinYin {

	public static String converterToPinYin(String chinese) {
		if(ToolsKit.isEmpty(chinese)) return "";
		String pinyinString = "";
		char[] charArray = chinese.toCharArray();
		// 根据需要定制输出格式，我用默认的即可
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 去掉声调数字
		try {
			// 遍历数组，ASC码大于128进行转换
			for (int i = 0; i < charArray.length; i++) {
				if (charArray[i] > 128) {
					// charAt(0)取出首字母
					pinyinString += PinyinHelper.toHanyuPinyinStringArray(charArray[i], defaultFormat)[0];// .charAt(0);
				} else {
					pinyinString += charArray[i];
				}
			}
			return pinyinString;
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
			return "";
		}
	}

	public static void main(String[] args) {
		// 输出“hyldNeekedgrbk”
		System.out.println(converterToPinYin("找不到相关订单"));

	}
}
